package Models;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Tanja
 */
public class RentalCollection
{
    private ArrayList<Rental> rentals;
    private static RentalCollection instance = new RentalCollection();

    private RentalCollection()
    {
        this.rentals = new ArrayList<Rental>();
		this.addRental(new Rental(MachineCollection.getInstance().getMachine(100),
									CustomerCollection.getInstance().getCustomer(101),
									500d, 20d));
		this.addRental(new Rental(MachineCollection.getInstance().getMachine(103),
									CustomerCollection.getInstance().getCustomer(102),
									200d, 40d));
		this.addRental(new Rental(MachineCollection.getInstance().getMachine(104),
									CustomerCollection.getInstance().getCustomer(105),
									300d, 30d));
    }

    public static RentalCollection getInstance()
    {
        return RentalCollection.instance;
    }

    public boolean addRental(Rental newRental)
    {
      return this.rentals.add(newRental);
    }

    public ArrayList<Rental> findRental(Date rentalDate, Date expectedReturnDate, Date returnDate, Machine machine, Customer customer, double deposit, double pricePerDay, double totalPrice)
    {
        ArrayList<Rental> foundRental = new ArrayList<Rental>();

        for(Rental rental : this.rentals)
        {
			if(rental.isActivated())
			{
				boolean found = false;
				if(rental.getRentalDate().equals(rentalDate))
				{ found = true; }
				else if(rental.getExpectedReturnDate().equals(expectedReturnDate))
				{ found = true; }
				else if(rental.getReturnDate() != null && rental.getReturnDate().equals(returnDate))
				{ found = true; }
				else if(rental.getCustomer().equals(customer))
				{ found = true; }
				else if(rental.getMachine().equals(machine))
				{ found = true; }
				else if(rental.getDeposit() == deposit)
				{ found = true; }
				else if(rental.getPricePerDay() == pricePerDay)
				{ found = true; }
				else if(rental.getTotalPrice() == totalPrice)
				{ found = true; }

				if(found == true)
				{
					foundRental.add(rental);
				}
			}
        }
        return foundRental;
    }

	public ArrayList<Rental> getAllRentals()
	{
		ArrayList<Rental> activatedRentals = new ArrayList<Rental>();
		for (Rental rental : this.rentals)
		{
			if(rental.isActivated())
			{
				activatedRentals.add(rental);
			}
		}
		return activatedRentals;
	}

    public Rental getRental(int id)
    {
        for(Rental rental : this.rentals)
        {
            if(rental.getId() == id)
            {
                return rental;
            }
        }
        return null;
    }
        

    public void returnRental(Rental rental)
    {        
       rental.setActivated(false);
	   rental.setReturnDate(Calendar.getInstance().getTime());
	   rental.getMachine().setAvailable(true);
    }
}
